package com.gulimall.product.app;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.gulimall.product.entity.ProductAttrValueEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.ProductAttrValueService;
import com.gulimall.product.vo.AttrRespVo;
import com.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-04-15 14:26:28
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 修改商品规格参数/product/attr/update/{spuId}
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> list){
        productAttrValueService.updateAttrSpu(spuId,list);
        return R.ok();
    }
    /**
     * 获取商品规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R getSpuAttr(@PathVariable("spuId") Long spuId ){
        List<ProductAttrValueEntity> entity=productAttrValueService.getListforSpu(spuId);
        return R.ok().put("data",entity);
    }

    /**
     * 条件查询
     */
    @GetMapping("/{saletype}/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("saletype") String saletype) {
        PageUtils page = attrService.querySearch(params, catelogId, saletype);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {            //使用AttrVo进行存储数据，可以存储额外的属性值
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo) {
//		attrService.updateById(attr);
        attrService.updatAttr(attrVo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
